package com.mingqijia.gassafety.shared.utils.message;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.gap.basic.exception.BizException;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.dto.cms.CmsInfoReqDTO;
import com.mingqijia.gassafety.shared.dto.cms.CmsInfoResDTO;
import com.mingqijia.gassafety.shared.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author StormyLiy
 * @version 1.0.0
 * @ClassName CmsMessageService.java
 * @createTime 2023年04月26日
 */
@Component
@Slf4j
public class CmsMessageService {
    @Value(value = "${asst.service.endpoint}")
    public String endpoint;

    /**
     * 站内信发送
     *
     * @param cmsReqParams
     * @return
     */
    public CmsInfoResDTO sendCmsMsg(JSONObject cmsReqParams) {
        log.info("url:"+endpoint);
        String result = HttpUtil.doPost(endpoint, Constants.RPC_METHOD, cmsReqParams, new HashMap<>());
        log.info("sendCmsMsg res:"+result);
        JSONObject cmsJSONObject = JSONUtil.parseObj(result).getJSONObject("result");
        CmsInfoResDTO cmsInfoResDTO = JSONUtil.toBean(cmsJSONObject, CmsInfoResDTO.class);
        return cmsInfoResDTO;
    }

    /**
     * 站内信参数封装
     *
     * @param msg
     * @param title
     * @param userId
     * @return
     */
    public JSONObject getCmsJSONParams(String msg, String title, Integer userId) {
        JSONObject jsonObject = JSONUtil.createObj().set("method", Constants.CMS_SEND_MSG);
        jsonObject.set(Constants.JSON_RPC, Constants.JSON_RPC_VERSION);
        jsonObject.set("id", UUID.randomUUID());
        JSONArray params = JSONUtil.createArray();
        CmsInfoReqDTO cmsInfoReqDTO = new CmsInfoReqDTO();
        cmsInfoReqDTO.setUser_id(userId);
        cmsInfoReqDTO.setTitle(title);
        cmsInfoReqDTO.setCate_id(5);
        cmsInfoReqDTO.setContent(msg);
        params.add(cmsInfoReqDTO);
        jsonObject.set("params", params);
        log.info("cmsInfoReqDTO：" + jsonObject);
        return jsonObject;
    }

    /**
     * 站内信参数封装
     *
     * @param msg
     * @param title
     * @param userId
     * @return
     */
    public JSONObject getCmsJSONParams(String msg, String title, Integer userId, String url) {
        JSONObject jsonObject = JSONUtil.createObj().set("method", Constants.CMS_SEND_MSG);
        jsonObject.set(Constants.JSON_RPC, Constants.JSON_RPC_VERSION);
        jsonObject.set("id", UUID.randomUUID());
        JSONArray params = JSONUtil.createArray();
        CmsInfoReqDTO cmsInfoReqDTO = new CmsInfoReqDTO();
        cmsInfoReqDTO.setUser_id(userId);
        cmsInfoReqDTO.setTitle(title);
        cmsInfoReqDTO.setCate_id(5);
        cmsInfoReqDTO.setContent(msg);
        cmsInfoReqDTO.setWap_link(url);
        params.add(cmsInfoReqDTO);
        jsonObject.set("params", params);
        log.info("cmsInfoReqDTO：" + jsonObject);
        return jsonObject;
    }

    /**
     * 站内信参数封装
     *
     * @param msg
     * @param title
     * @param userId
     * @param wapLink
     * @param appLInk
     * @return
     */
    public JSONObject getCmsJSONParams(String msg, String title, Integer userId, String wapLink, String appLInk) {
        JSONObject jsonObject = JSONUtil.createObj().set("method", Constants.CMS_SEND_MSG);
        jsonObject.set(Constants.JSON_RPC, Constants.JSON_RPC_VERSION);
        jsonObject.set("id", UUID.randomUUID());
        JSONArray params = JSONUtil.createArray();
        CmsInfoReqDTO cmsInfoReqDTO = new CmsInfoReqDTO();
        cmsInfoReqDTO.setUser_id(userId);
        cmsInfoReqDTO.setTitle(title);
        cmsInfoReqDTO.setCate_id(5);
        cmsInfoReqDTO.setContent(msg);
        cmsInfoReqDTO.setWap_link(wapLink);
        cmsInfoReqDTO.setApp_link(appLInk);
        params.add(cmsInfoReqDTO);
        jsonObject.set("params", params);
        log.info("cmsInfoReqDTO：" + jsonObject);
        return jsonObject;
    }
}